package com.lc.cehui.ui.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Paint;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.gyf.immersionbar.ImmersionBar;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lc.cehui.R;
import com.lc.cehui.base.BaseAdapter;
import com.lc.cehui.base.BaseDialog;
import com.lc.cehui.base.TitleBarFragment;
import com.lc.cehui.dialog.MenuDialog;
import com.lc.cehui.http.glide.GlideApp;
import com.lc.cehui.http.request.IndexApi;
import com.lc.cehui.http.request.IndexBean;
import com.lc.cehui.http.request.IndexSearchApi;
import com.lc.cehui.http.request.SendsRuleApi;
import com.lc.cehui.http.response.GoodsTypeBean;
import com.lc.cehui.http.response.IndexSearchBean;
import com.lc.cehui.other.IntentKey;
import com.lc.cehui.ui.activity.EnterpriseListActivity;
import com.lc.cehui.ui.activity.GoodsDetailsActivity;
import com.lc.cehui.ui.activity.MainActivity;
import com.lc.cehui.ui.activity.PersonnelListActivity;
import com.lc.cehui.ui.activity.home.AbutmentActivity;
import com.lc.cehui.ui.activity.home.AccessoriesActivity;
import com.lc.cehui.ui.activity.home.AgencyActivity;
import com.lc.cehui.ui.activity.home.BiogenesisActivity;
import com.lc.cehui.ui.activity.home.CommissionActivity;
import com.lc.cehui.ui.activity.home.CooperationActivity;
import com.lc.cehui.ui.activity.home.InformationActivity;
import com.lc.cehui.ui.activity.home.InstrumentActivity;
import com.lc.cehui.ui.activity.home.IntelligenceActivity;
import com.lc.cehui.ui.activity.home.IntelligenceSecondActivity;
import com.lc.cehui.ui.activity.home.MessageDetailActivity;
import com.lc.cehui.ui.activity.home.ProjectDockActivity;
import com.lc.cehui.ui.activity.home.RecruitmentActivity;
import com.lc.cehui.ui.activity.home.SkillTrainingActivity;
import com.lc.cehui.ui.activity.home.SoftActivity;
import com.lc.cehui.ui.activity.home.SoftwareSalesActivity;
import com.lc.cehui.ui.activity.home.TrainingActivity;
import com.lc.cehui.ui.activity.home.search.ExhibitionActivity;
import com.lc.cehui.ui.adapter.ImageAdapter;
import com.lc.cehui.util.SPUtil;
import com.lc.cehui.widget.VerticalTextView;
import com.lc.cehui.widget.XCollapsingToolbarLayout;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.youth.banner.Banner;
import com.youth.banner.config.IndicatorConfig;
import com.youth.banner.listener.OnBannerListener;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.OnClick;

public class HomeFragment extends TitleBarFragment<MainActivity>
        implements XCollapsingToolbarLayout.OnScrimsListener, OnRefreshLoadMoreListener,
        BaseAdapter.OnChildClickListener {

    @BindView(R.id.ctl_home_bar)
    XCollapsingToolbarLayout mCollapsingToolbarLayout;
    @BindView(R.id.tv_home_hint)
    TextView mHintView;
    @BindView(R.id.tb_home_title)
    Toolbar mToolbar;

    @BindView(R.id.verticalTv)
    VerticalTextView verticalTv;

    @BindView(R.id.kind_recy)
    RecyclerView kindRecy;
    @BindView(R.id.rl_status_refresh)
    SmartRefreshLayout mRefreshLayout;
    @BindView(R.id.rv_status_list)
    RecyclerView rvStatusList;
    @BindView(R.id.banner)
    Banner banner;
    private List<IndexSearchBean.DataBean> searchList = new ArrayList<>();
    private List<String> dataList = new ArrayList<>();
    private BaseQuickAdapter<IndexBean.DataBean.TypeBean, BaseViewHolder> kindadapter;
    private BaseQuickAdapter<IndexBean.DataBean.GoodsBean, BaseViewHolder> mingqibottomadpter;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView() {

        kindRecy.setLayoutManager(new GridLayoutManager(getActivity(), 4));
        kindadapter = new BaseQuickAdapter<IndexBean.DataBean.TypeBean, BaseViewHolder>(R.layout.item_home_top) {
            @Override
            protected void convert(@NotNull BaseViewHolder baseViewHolder, IndexBean.DataBean.TypeBean bean) {
                baseViewHolder.setText(R.id.tv_menu_title_home, bean.getTitle());
                GlideApp.with(requireContext()).load(bean.getPicurl()).apply(new RequestOptions().placeholder(R.drawable.image11).error(R.drawable.image11)).into((ImageView) baseViewHolder.getView(R.id.iv_menu_home));
            }
        };
        kindRecy.setAdapter(kindadapter);
        kindadapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull @NotNull BaseQuickAdapter<?, ?> adapter, @NonNull @NotNull View view, int position) {
                switch (kindadapter.getData().get(position).getTitle()) {
                    case "企业招聘":
//
                        startActivity(new Intent(getContext(),RecruitmentActivity.class).putExtra("id",kindadapter.getData().get(position).getId()+""));
                        break;
                    case "校企合作":
//                   校企合作
                        startActivity(new Intent(getContext(),CooperationActivity.class).putExtra("id",kindadapter.getData().get(position).getId()+""));
                        break;
                    case "企业资讯":

//                         资源共享

                        startActivity(new Intent(getContext(),IntelligenceActivity.class).putExtra("id",kindadapter.getData().get(position).getId()+""));
                        break;
                    case "项目对接":
//                    项目对接

                        my_sends_rule(kindadapter.getData().get(position).getId(),"1");


                        break;
                    case "代办管家":
//                   代办管家
                        startActivity(new Intent(getContext(),CommissionActivity.class).putExtra("id",kindadapter.getData().get(position).getId()+""));

                        break;
                    case "技能培训":
//                      技能培训

                        startActivity(new Intent(getContext(),TrainingActivity.class).putExtra("id",kindadapter.getData().get(position).getId()+""));

                        break;
                    case "软件销售":
//                  书记软著
                        startActivity(new Intent(getContext(),SoftActivity.class).putExtra("id",kindadapter.getData().get(position).getId()+""));


                        break;
                    case "仪器/配件销售":
//                      仪器配件
                        startActivity(new Intent(getContext(),AccessoriesActivity.class).putExtra("id",kindadapter.getData().get(position).getId()+""));


                        break;
                }
            }
        });
        rvStatusList.setLayoutManager(new GridLayoutManager(getActivity(), 2));
        mingqibottomadpter = new BaseQuickAdapter<IndexBean.DataBean.GoodsBean, BaseViewHolder>(R.layout.item_index_goods) {
            @Override
            protected void convert(@NotNull BaseViewHolder baseViewHolder, IndexBean.DataBean.GoodsBean bean) {
                baseViewHolder.setText(R.id.title_name, bean.getName());
                GlideApp.with(requireContext()).load(bean.getPicurl()).apply(new RequestOptions().placeholder(R.drawable.image11).error(R.drawable.image11)).into((ImageView) baseViewHolder.getView(R.id.goods_img));

                RecyclerView recyclerView=baseViewHolder.getView(R.id.rv_coupon);
                LinearLayoutManager lm = new LinearLayoutManager(getContext());
                lm.setOrientation(LinearLayoutManager.HORIZONTAL);
                recyclerView.setLayoutManager(lm);
                BaseQuickAdapter<IndexBean.DataBean.GoodsBean.CouponBean,BaseViewHolder> mAdapter=new BaseQuickAdapter<IndexBean.DataBean.GoodsBean.CouponBean, BaseViewHolder>(R.layout.item_index_coupon,bean.getCoupon()) {
                    @Override
                    protected void convert(@NotNull BaseViewHolder holder, IndexBean.DataBean.GoodsBean.CouponBean couponBean) {
                                holder.setText(R.id.tv_coupon,"满"+couponBean.getShold_price()+"减"+couponBean.getRed_price());
                    }
                };
                recyclerView.setAdapter(mAdapter);
                baseViewHolder.setText(R.id.tv_preprice, bean.getY_price());
                TextView price=baseViewHolder.getView(R.id.tv_price);
                price.setText("¥"+bean.getPrice());
                price.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG);
            }
        };
        rvStatusList.setAdapter(mingqibottomadpter);
         mingqibottomadpter.setOnItemClickListener(new OnItemClickListener() {
             @Override
             public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                 startActivity(new Intent(getContext(), GoodsDetailsActivity.class).putExtra("id",mingqibottomadpter.getData().get(position).getGoods_id()));
             }
         });
        // 给这个 ToolBar 设置顶部内边距，才能和 TitleBar 进行对齐
        ImmersionBar.setTitleBar(getAttachActivity(), mToolbar);

        index();
        index_search();
    }
    private void index_search(){
        EasyHttp.post(this)
                .api(new IndexSearchApi().setPage(1))
                .request(new HttpCallback<IndexSearchBean>(this) {

                    @Override
                    public void onSucceed(IndexSearchBean data) {
                        searchList.clear();
                        searchList.addAll(data.getData());
                    }
                });
    }
    private void index() {
        EasyHttp.post(this)
                .api(new IndexApi())
                .request(new HttpCallback<IndexBean>(this) {

                    @Override
                    public void onSucceed(IndexBean data) {
                        banner.setAdapter(new ImageAdapter(getContext(),data.getData().getBanner())).setLoopTime(5000).setIndicatorGravity(IndicatorConfig.Direction.CENTER).setOnBannerListener(new OnBannerListener() {
                            @Override
                            public void OnBannerClick(Object datas, int position) {

                            }
                        });
                        initVText(data.getData().getNotice());
                        kindadapter.setNewData(data.getData().getType());
                        kindadapter.notifyDataSetChanged();
                        mingqibottomadpter.setNewData(data.getData().getGoods());
                        mingqibottomadpter.notifyDataSetChanged();
                    }
                });
    }

    private void initVText(List<IndexBean.DataBean.NoticeBean>  list) {
        ArrayList<String> data = new ArrayList<String>();
        for (int i = 0; i <list.size() ; i++) {
            data.add(list.get(i).getTitle());
        }

//
        verticalTv.setTextList(data);
        verticalTv.setText(14, 10, ContextCompat.getColor(requireActivity(), R.color.color333333));
        verticalTv.setTextStillTime(4000);
        verticalTv.setAnimTime(200);
        verticalTv.startAutoScroll();
        verticalTv.setOnItemClickListener(new VerticalTextView.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                //跳转到头条详情
                startActivity(new Intent(getContext(), MessageDetailActivity.class).putExtra("notice_id", list.get(position).getNotice_id() + ""));
            }
        });


    }

    @Override
    protected void initData() {

    }


//

    @SuppressLint("RestrictedApi")
    @Override
    public void onScrimsStateChange(XCollapsingToolbarLayout layout, boolean shown) {
        if (shown) {


        } else {


        }
    }

    @Override
    public void onLoadMore(@NonNull @NotNull RefreshLayout refreshLayout) {

    }

    @Override
    public void onRefresh(@NonNull @NotNull RefreshLayout refreshLayout) {

    }

    @Override
    public void onChildClick(RecyclerView recyclerView, View childView, int position) {

    }

    public void showMenuDialog(List<String> dataList) {
        new MenuDialog.Builder(getActivity())
                .setList(dataList)
                .setTitle("请选择要搜索的分类")
                .setListener(new MenuDialog.OnListener<String>() {
                    @Override
                    public void onSelected(BaseDialog dialog, int position, String string) {
                        switch (position) {
                            case 0:
//                                企业
                                startActivity(new Intent(getContext(),EnterpriseListActivity.class).putExtra("id",""));

                                break;
                            case 1:
//                                人才

                                startActivity(new Intent(getContext(),PersonnelListActivity.class).putExtra("id",""));
                                break;
                            case 2:
//                                生源
                                startActivity(new Intent(getContext(),BiogenesisActivity.class).putExtra("id",""));


                                break;
                            case 3:
                                startActivity(new Intent(getContext(),InformationActivity.class).putExtra("id",""));
//                                资讯

//
                                break;
                            case 4:
//                                资源共享
                                startActivity(new Intent(getContext(),IntelligenceSecondActivity.class).putExtra("id",""));

//

                                break;
                            case 5:
//                                展会信息
                                startActivity(new Intent(getContext(),ExhibitionActivity.class).putExtra("id",""));

//

                                break;
                            case 6:
                                Log.e("TAG", "onSelected: "+position +"---"+ JSONObject.toJSONString(searchList));
//                                项目对接
                                my_sends_rule( searchList.get(position).getType_pid(),"2");



//

                                break;
                            case 7:
//                                资质代办
                                startActivity(new Intent(getContext(),AgencyActivity.class).putExtra("pid",searchList.get(position).getType_pid()+"").putExtra("id",""));



                                break;
                            case 8:
                                startActivity(new Intent(getContext(),SkillTrainingActivity.class).putExtra("id",""));


//                                技能培训

                                break;
                            case 9:
                                startActivity(new Intent(getContext(),SoftwareSalesActivity.class).putExtra("pid",searchList.get(position).getType_pid()+"").putExtra("id",""));



//                                软件销售
//
                                break;
                            case 10:
//                                仪器配件
                                startActivity(new Intent(getContext(),InstrumentActivity.class).putExtra("pid",searchList.get(position).getType_pid()+"").putExtra("id",""));


                                break;
                        }
                    }

                    @Override
                    public void onCancel(BaseDialog dialog) {

                    }
                }).show();
    }

    private void my_sends_rule(int type_id,String type) {
        EasyHttp.post(this)
                .api(new SendsRuleApi()
                        .setUserid(SPUtil.get(getContext(), IntentKey.USERID, 0))
                        .setType_pid(type_id))
                .request(new HttpCallback<GoodsTypeBean>(this) {
                    @Override
                    public void onSucceed(GoodsTypeBean data) {
                        if (callBackCode(data)) {
                            if (type.equals("1")){
                                startActivity(new Intent(getContext(),AbutmentActivity.class).putExtra("id",type_id+""));
                            }else {
                                startActivity(new Intent(getContext(), ProjectDockActivity.class).putExtra("id", ""));
                            }
                        }
                    }
                });
    }

    @OnClick({R.id.ll_search, R.id.sb_tuijian})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_search:
                dataList.clear();
                for (int i = 0; i < searchList.size(); i++) {

                    dataList.add(searchList.get(i).getTitle());
                }
                showMenuDialog(dataList);
                break;
            case R.id.sb_tuijian:
                break;
        }
    }
}
